<template>
	<view class="container">
		<view class="item"  v-for="(item, index) in items" :key="index" @tap="goToBind(item.id)">
			<text>社区名称:{{ item.communityName }}</text>
			<text>联系人:{{ item.contacts }}</text>
			<text>电话:{{ item.telephone }}</text>
			<text>地址:{{ item.showAddress }}</text>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			deviceId: '',
			items: []		
		};
	},
	onLoad(e) {
		this.deviceId = e.scene;
		this.$http
			.request({
				url: 'api/Community/CheckIsAdmin'
			})
			.then(res => {
				// #ifdef H5
				this.$wechat.hideMenu();
				// #endif
			
				if (res) {
					this.$http
						.request({
							url: 'api/Community/GetCommunities'
						})
						.then(res => {
							this.items = res;
						});
				} else {
					uni.navigateTo({						
						url: '../add?deviceId='+this.deviceId
					});
				}
			});
	},
	methods: {
		goToBind(id) {
			var that = this;
			uni.showModal({
				content: '是否确定要绑定该社区？绑定后不能修改',
				showCancel: true,
				success(res) {
					if (res.confirm) {
						that.$http
							.request({
								url: 'api/Community/BindCommunity',
								method: 'POST',
								data: {
									deviceId: that.deviceId,
									communityId: id
								}
							})
							.then(() => {
								uni.showToast({
									title: '绑定成功',
									success() {
										setTimeout(function() {
											uni.redirectTo({
												url: '../list'
											});
										}, 1500);
									}
								});
							});
					}
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.container {
	background: $uni-bg-color-grey;
	padding-bottom: 10rpx;
}
.item {
	display: flex;
	flex-direction: column;
	width: 90%;
	margin: 0 auto;
	background: #ffffff;
	padding: 20rpx;
	border-radius: 15rpx;
	line-height: 50rpx;
	margin-bottom: 20rpx;
}
</style>
